package cn.asoul.infrastructure.persistent.repository;

import cn.asoul.domain.strategy.model.entity.StrategyAwardEntity;
import cn.asoul.domain.strategy.repostory.IStrategyRepository;
import cn.asoul.infrastructure.persistent.dao.IStrategyAwardDao;
import cn.asoul.infrastructure.persistent.po.StrategyAward;
import cn.asoul.infrastructure.persistent.redis.IRedisService;
import cn.asoul.types.common.Constants;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 策略仓储层
 * @author: chen
 * @date: 2025/3/6 15:36
 */
@Repository
@Slf4j
@RequiredArgsConstructor
public class StrategyRepository implements IStrategyRepository {
    private final IRedisService redisService;
    private final IStrategyAwardDao strategyAwardDao;


    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId){
        String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
        List<StrategyAwardEntity> strategyAwardEntityList = redisService.getValue(cacheKey);
        if (strategyAwardEntityList != null && !strategyAwardEntityList.isEmpty())
            return strategyAwardEntityList;

        List<StrategyAward> strategyAwards = strategyAwardDao.queryStrategyAwardListByStrategyId(strategyId);
        strategyAwardEntityList = strategyAwards.stream()
                .map(strategyAward -> StrategyAwardEntity.builder()
                        .strategyId(strategyAward.getStrategyId())
                        .awardId(strategyAward.getAwardId())
                        .awardCount(strategyAward.getAwardCount())
                        .awardCountSurplus(strategyAward.getAwardCountSurplus())
                        .awardRate(strategyAward.getAwardRate())
                        .build()).collect(Collectors.toList());
        redisService.setValue(cacheKey, strategyAwardEntityList);
        return strategyAwardEntityList;
    }

    @Override
    public void storeStrategyAwardSearchRateTable(Long strategyId, HashMap<Integer, Integer> shuffledStrategyAwardSearchRateTable) {
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId, shuffledStrategyAwardSearchRateTable.size());
        RMap<Object, Object> cacheMap = redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId);
        if (!cacheMap.isEmpty()) {
            cacheMap.clear();
        }
        shuffledStrategyAwardSearchRateTable.forEach(cacheMap::fastPut);
    }

    @Override
    public Integer getRateRange(Long strategyId) {
        return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY + strategyId);
    }

    @Override
    public Integer getStrategyAwardAssemble(Long strategyId, int rateKey) {
        return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId, rateKey);
    }
}
